<template>
<Table :columns="columns" :data="data" />
</template>

<script>
import qs from 'qs';
import { Table, Message } from 'iview';
import ajaxiView from '@/utils/ajaxiView';
import auth from '@/utils/auth';

export default {
  props: ['roles_id','accesses','access_list'],
  components: {
    [Table.name]: Table
  },
  data() {
    return {
      token: auth.getToken(),
      columns: [
        {title: 'ID', key: 'id', align: 'center'},
        {title: '名称', key: 'name', align: 'center'},
        {title: '描述', key: 'description', align: 'center'},
        {title: '操作', key: 'action', align: 'center', render: (h, params) => {
          var _this = this;
          var opeare = [];
          if(
            typeof(this.$props.access_list) === 'undefined' ||
            typeof(this.$props.access_list[params.row.id]) === 'undefined' ||
            this.$props.access_list[params.row.id].allowed.toString() === '0'
          ) {
            opeare.push(h('a', {
              class: 'ivu-btn ivu-btn-primary ivu-btn-small',
              on: {
                  click() {
                    ajaxiView.put('/backstage/account/roles/'+_this.$props.roles_id+'/resources/'+params.row.resources_id+'?token='+_this.token, qs.stringify({access_id:params.row.id,allowed:1})).then(function(data) {
                      if(data !== false) {
                        Message.success({
                          content: data.msg,
                          onClose: function() {
                            window.location.reload();
                          }
                        });

                      }
                    });
                  }
                }
            }, '允许'));
          } else {
            opeare.push(h('a', {
              class: 'ivu-btn ivu-btn-error ivu-btn-small',
              on: {
                  click() {
                    ajaxiView.put('/backstage/account/roles/'+_this.$props.roles_id+'/resources/'+params.row.resources_id+'?token='+_this.token, qs.stringify({access_id:params.row.id,allowed:0})).then(function(data) {
                      if(data !== false) {
                        Message.success({
                          content: data.msg,
                          onClose: function() {
                            window.location.reload();
                          }
                        });

                      }
                    });
                  }
                }
            }, '禁止'));
          }

          return h('div', opeare);
        }}
      ],
      data: []
    };
  },
  mounted() {
    for(let i in this.$props.accesses) {
      this.data.push(this.$props.accesses[i]);
    }
  }
};

</script>
